package yf.mall.controller.api;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.service.WxWebService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import yf.mall.entity.AccountEntity;
import yf.mall.entity.LevelEntity;
import yf.mall.entity.UserEntity;
import com.yf.response.R;
import yf.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.vo.UserEntityVo;
import yf.mall.vo.UserVo;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/api/user")
@RestController
public class ApiUserController {

    @Autowired
    private NoticeService noticeService;

    /**
     * TODO 接入Redis存储验证码，接入短信发送验证码
     * 生成验证码,发送
     *
     * @param mobile
     * @return
     */
    @RequestMapping("/captcha")
    @IgnoreAuth
    public R captcha(String mobile) {
        String captcha = noticeService.sendCaptcha(mobile);
        return R.ok(captcha);
    }

    @Autowired
    private WxWebService wxWebService;

    /**
     * 获取公众号二维码
     *
     * @param userId
     * @return
     */
    @RequestMapping("/qr/code")
    public R getQrCode(@LoginUserId Long userId) {
        String url = wxWebService.getQrCodeUrl(String.valueOf(userId));
        return R.ok(url);
    }

    @RequestMapping("/auth/img")
    @IgnoreAuth
    public void authImg(HttpServletResponse response, Long userId) {
        userService.createAuthImg(userId, response);
    }

    /**
     * TODO 绑定以及生成代理编号
     * 绑定手机号码(可以此时发放)
     *
     * @param userId
     * @param mobile
     * @return
     */
    @RequestMapping("/mobile")
    public R mobile(@LoginUserId Long userId, String mobile) {
        return R.ok();
    }


    /**
     * 根据手机号或者编号或者名字或者电话查询
     *
     * @return
     */
    @RequestMapping("/search")
    @IgnoreAuth
    public R search(@LoginUserId Long userId, String param) {
        List<UserVo> userEntities = userService.findByNameOrCodeOrMobile(param);
        return R.ok(userEntities);
    }


    /**
     * 查询下级
     * type 0 无限层
     *
     * @return
     */
    @RequestMapping("/top/search")
    public R searchByTop(@LoginUserId Long userId, @RequestParam(name = "type") Integer type) {
        List<UserVo> userEntities = userService.findByTop(userId, type);
//        List<UserEntityVo> userEntities = userService.findByTop(userId, type);
        UserEntity user = userService.get(userId);
        UserEntity topUser = userService.get(user.getPid());
        return R.ok(userEntities).putExtra("topUser", topUser).putExtra("user", user);
    }

    @RequestMapping("/update")
    public R update(@LoginUserId Long userId, String phone, String password, String name, Boolean sync) {
        UserEntity userEntity = userService.update(userId, phone, password, name, sync);
        return R.ok(userEntity);
    }

    /**
     * 查找用户信息
     * TODO 限制只有团队长能看团队下面的
     *
     * @param userId
     * @param uid
     * @return
     */
    @RequestMapping("/info")
    public R info(@LoginUserId Long userId, Long uid) {
        UserEntity userEntity;
        LevelEntity levelEntity = null;
        List<AccountEntity> accountEntities;
        String  topCode= "";
        BigDecimal waitIncome = new BigDecimal(0); //代收款
        //查找别人
        if (uid != null) {
            UserEntity user = this.userService.get(userId);
            userEntity = userService.get(uid);
            Long pid = user.getPid();
            levelEntity = levelService.get(userEntity.getLevelId());
          if (pid.equals(uid)){
              levelEntity.setName("**");
          }

            accountEntities = accountService.findByUserId(uid);
        } else {//查找自己
            userEntity = userService.get(userId);
            levelEntity = levelService.get(userEntity.getLevelId());
            accountEntities = accountService.findByUserId(userId);
            topCode = userService.get(userEntity.getPid()).getCode();
            waitIncome = accountRecordService.waitIncome(userId);
        }
        return R.ok(userEntity).putExtra("level", levelEntity).putExtra("accounts", accountEntities).putExtra("topCode",topCode).putExtra("waitIncome",waitIncome);
    }

    @RequestMapping("/nowUserInfo")
    public R nowUserInfo(@LoginUserId Long userId) {
        UserEntityVo userEntityVo = this.userService.findNowUserInfo(userId);
        return R.ok(userEntityVo);
    }


    @RequestMapping("/levels")
    public R Level() {
        List<LevelEntity> levelEntities = levelService.findlist();
        return R.ok(levelEntities);
    }

    @RequestMapping("/level")
    public R levels(@LoginUserId Long userId) {
        List<LevelEntity> levelEntities = levelService.findLowLevel(userId);
        return R.ok(levelEntities);
    }

    @RequestMapping("/level/change")
    public R changeLevel(@LoginUserId Long userId, Long toUserId, Long levelId) {
        LevelEntity levelEntity = levelService.changeLevel(userId, toUserId, levelId);
        return R.ok(levelEntity);
    }


    @Autowired
    private UserService userService;

    @Autowired
    private LevelService levelService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountRecordService accountRecordService;

}
